/*
Chris Elrod
10/27/2008
CSci 252
Prof. Hasan

Project 2 - EJB

Reference: 
    BEA trader example: BEA 10.3 HOME\wlserver_10.3\samples\server\examples\src\examples\ejb\ejb20\basic\statefulSession
*/

package examples.ejb.ejb20.basic.Project2;

import java.rmi.RemoteException;
import java.util.Hashtable;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;


/**
 * This class illustrates calling a stateless Session Bean and performing
 * the following exercises:
 * 
 *  Creates a Facade.
 *  Tests Facade's methods required for project.
 *  Removes the Facade.
 */


public class Client
{


  private String url;
  private static final String JNDI_NAME = "Project2-Facade-FacadeHome";
  private FacadeHome home;


  public Client(String url) throws NamingException {
    this.url = url;
    home = lookupHome();
  }


  public static void main(String[] args) throws Exception {

    log("\nBeginning Facade.Client...\n");

    String url       = "t3://localhost:7001";
    Client client = null;

    try {
      client = new Client(url);
      client.testFacade();
    } catch (NamingException ne) {
      log("Unable to look up the beans home: " + ne.getMessage());
      throw ne;
    } catch (Exception e) {
      log("There was an exception while creating and using the Facade.");
      log("This indicates that there was a problem communicating with the server: "+e);
      throw e;
    }

    log("\nEnding Facade.Client...\n");
  }


  /**
   * Runs this example.
   */

  public void testFacade()
    throws CreateException, RemoteException, RemoveException
  {
    // 1. Create a Facade.
    log("Creating a Facade for testFacade().\n");
    Facade Facade = (Facade) narrow(home.create(), Facade.class);

    log("\t Testing Facade's SecurityBean work:");

    log("\n\t\t" + "createUser(\"Omair33333\",\"ert56y7\"), result is: " + Facade.createUser("Omair33333","ert56y7") +"!");  
    log("\t\t" + "createUser(\"John44\",\"1ert4t5o\"), result is: " + Facade.createUser("John44","1ert4t5o") +"!");  
    log("\t\t" + "createUser(\"Tim\",\"er5t\"), result is: " + Facade.createUser("Tim","er5t") +"!");  
    log("\t\t" + "createUser(\"Steve4\",\"yuT6%rt\"), result is: " + Facade.createUser("Steve4","yuT6%rt") +"!");  
    log("\t\t" + "createUser(\"BillyBob\",\"eiiiiiiiii9\"), result is: " + Facade.createUser("BillyBob","eiiiiiiiii9") +"!");  
    log("\t\t" + "createUser(\"Johnny\",\"1999999999aaaaa\"), result is: " + Facade.createUser("Johnny","1999999999aaaaa") +"!");  
    log("\t\t" + "createUser(\"John44\",\"1ert4t5o\"), result is: " + Facade.createUser("John44","1ert4t5o") +"!");  

    log("\n\t\t" + "authenticate(\"Omair33333\",\"ert56y7\"), result is: " + Facade.authenticate("Omair33333","ert56y7") +"!"); 
    log("\t\t" + "authenticate(\"John44\",\"1ert4t5o\"), result is: " + Facade.authenticate("John44","1ert4t5o") +"!");  
    log("\t\t" + "authenticate(\"Omair33333\",\"ert5y7\"), result is: " + Facade.authenticate("Omair33333","ert5y7") +"!"); 
    log("\t\t" + "authenticate(\"John44\",\"1ert4to\"), result is: " + Facade.authenticate("John44","1ert4to") +"!");  

    log("\n\t Testing Facade's ProfileBean work:");

    log("\n\t\t" + "addProfile(\"test23\", \"12727 Georgia Ave., Washington Dc., 11111\"), results in: " + Facade.addProfile("test23","12727 Georgia Ave., Washington Dc., 11111") );           
    log("\n\t\t" + "getAddress(Omair), results in: " + Facade.getAddress("Omair") );          
    log("\t\t" + "getAddress(John34), results in: " + Facade.getAddress("John34") );                      
    log("\t\t" + "getAddress(test23), results in: " + Facade.getAddress("test23") );   

    log("\n\t Testing Facade's TransactionBean work:");

    //Need to call reject and approve, intertwined with these addRequest.

    log("\n\t\t" + "addRequest( \"*Mastering EJB*23455*2*John Smith*123 Main St. Reston VA 20133*GW*\"  ), result is: " + Facade.addRequest("*Mastering EJB*23455*2*John Smith*123 Main St. Reston VA 20133*GW*") );          
    log("\t\t" + "addRequest( \"*Computer Networks*12345*1*Rob Tim*2121 I St. Washington DC*IBM*\"  ), result is: " + Facade.addRequest("*Computer Networks*12345*1*Rob Tim*2121 I St. Washington DC*IBM*") );          
    log("\t\t" + "addRequest( \"*Artificial Intelligence*98347*45*Johnny Good*738 Bill St. Rockville MD*Mary U*\"  ), result is: " + Facade.addRequest("*Artificial Intelligence*98347*45*Johnny Good*738 Bill St. Rockville MD*Mary U*") );          
    log("\t\t" + "addRequest( \"*Database Design*89384*55*Bill Battles*89422 I Street. Baltimore MD*Johns Hopkins*\"  ), result is: " + Facade.addRequest("*Database Design*89384*55*Bill Battles*89422 I Street. Baltimore MD*Johns Hopkins*") );          
    log("\t\t" + "addRequest( \"*Security for Web 2.0*98347*10*Vijay Sathmamamamtama*4321 Layhill Rd. Woodbridge Va*Google*\"  ), result is: " + Facade.addRequest("*Computer Networks*12345*1*Rob Tim*2121 I St. Washington DC*IBM*") );          
    log("\t\t" + "addRequest( \"*Operating Systems*45982*33*Rob Tompson*2222 Carrol Avenue Thurmont MD*Maryland University*\"  ), result is: " + Facade.addRequest("*Security for Web 2.0*98347*10*Vijay Sathmamamamtama*4321 Layhill Rd. Woodbridge Va*Google*") );          

    
    log("\n\t\t" + "getIds(3).toString() = " + Facade.getIds().toString() );
    log("\n\t\t" + "getTitle(3) = " + Facade.getTitle(3) );
    log("\n\t\t" + "getISBN(\"Database Design\") = " + Facade.getISBN("Database Design") + " - This is a Facade.Transaction hook.");
    log("\n\t\t" + "getQuantity(3) = " + Facade.getQuantity(3) );
    log("\n\t\t" + "getName(3) = " + Facade.getName(3) );
    log("\n\t\t" + "getAddress(3) = " + Facade.getAddress(3) );
    log("\n\t\t" + "getOrganization(3) = " + Facade.getOrganization(3) );

    log("\n\t\t" + "approveTransaction( \"1\" ), result is: " + Facade.approveTransaction( "1" ) );          
    log("\t\t" + "approveTransaction( \"2\" ), result is: " + Facade.approveTransaction( "2" ) );
    log("\t\t" + "approveTransaction( \"3\" ), result is: " + Facade.approveTransaction( "3" ) ); 
    log("\n\t\t" + "rejectTransaction( \"4\" ), result is: " + Facade.rejectTransaction( "4" ) );          
    log("\t\t" + "rejectTransaction( \"5\" ), result is: " + Facade.rejectTransaction( "5" ) );
    log("\t\t" + "rejectTransaction( \"6\" ), result is: " + Facade.rejectTransaction( "6" ) );       

    // 3. Remove the Security.
    log("\nRemoving the Facade for testFacade()");
    Facade.remove();
  }


  /**
   * RMI/IIOP clients should use this narrow function
   */
  private Object narrow(Object ref, Class c) {
    return PortableRemoteObject.narrow(ref, c);
  }


  /**
   * Lookup the EJBs home in the JNDI tree
   */
  private FacadeHome lookupHome() throws NamingException {
    // Lookup the beans home using JNDI
    Context ctx = getInitialContext();

    try {
      Object home = ctx.lookup(JNDI_NAME);
      return (FacadeHome) narrow(home, FacadeHome.class);
    } catch (NamingException ne) {
      log("The client was unable to lookup the EJBHome.  Please make sure ");
      log("that you have deployed the ejb with the JNDI name "+
        JNDI_NAME+" on the WebLogic server at "+url);
      throw ne;
    }
  }


  /**
   * Using a Properties object will work on JDK 1.1.x and Java2
   * clients
   */
  private Context getInitialContext() throws NamingException {
    
    try {
      // Get an InitialContext
      Hashtable<String,String> h = new Hashtable<String,String>();
      h.put(Context.INITIAL_CONTEXT_FACTORY,
        "weblogic.jndi.WLInitialContextFactory");
      h.put(Context.PROVIDER_URL, url);
      return new InitialContext(h);
    } catch (NamingException ne) {
      log("We were unable to get a connection to the WebLogic server at "+url);
      log("Please make sure that the server is running.");
      throw ne;
    }
  }


  private static void log(String s) { System.out.println(s); }
  
}
